package com.biz.primus.ms.commodity.api;

import com.biz.primus.base.enums.vendor.VendorType;
import com.biz.primus.base.vo.KeywordWeightVo;
import com.biz.primus.base.vo.ProductFieldVo;
import com.biz.primus.base.vo.promotion.PromotionProductReqVo;
import com.biz.primus.base.vo.promotion.PromotionProductVo;
import com.biz.primus.commodity.vo.CommodityVo;
import com.biz.primus.commodity.vo.SyncCommoditySpecVo;
import com.biz.primus.commodity.vo.SyncCommodityVo;
import com.biz.primus.commodity.vo.backend.CommodityItemVo;
import com.biz.primus.commodity.vo.backend.CommodityQueryVo;
import com.biz.primus.commodity.vo.backend.PlatformCommodityDetailVo;
import com.biz.primus.commodity.vo.frontend.ProductRelevantDetail;
import com.biz.primus.common.vo.SpringPageVO;
import com.biz.primus.ms.base.api.BaseApiController;
import com.biz.primus.ms.commodity.dao.redis.ProductRedisDao;
import com.biz.primus.ms.commodity.service.backend.ExtendService;
import com.biz.primus.ms.commodity.service.backend.KeywordWeightService;
import com.biz.primus.ms.commodity.service.frontend.CommodityService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("soa/commodityService")
public class CommodityApiController extends BaseApiController{


    public CommodityApiController(CommodityService commodityService, ExtendService extendService, KeywordWeightService keywordWeightService, ProductRedisDao productRedisDao) {
        this.commodityService = commodityService;
        this.extendService = extendService;
        this.keywordWeightService = keywordWeightService;
        this.productRedisDao = productRedisDao;
    }

    private final CommodityService commodityService;

    private final ExtendService extendService;

    private final KeywordWeightService keywordWeightService;

    private final ProductRedisDao productRedisDao;

    @ApiOperation("分页查询显示商品列表")
    @PostMapping("findAll")
    SpringPageVO<CommodityItemVo> findAll(@RequestBody CommodityQueryVo reqVo){
        return commodityService.findAll(reqVo);
    }

    @ApiOperation("后台获取商品详情")
    @GetMapping("getCommodityDetailVo")
    PlatformCommodityDetailVo getCommodityDetailVo(Long productId){
        return commodityService.getCommodityDetailVo(productId);
    }

    @ApiOperation("促销查询商品列表")
    @PostMapping("findAllToPromotion")
    SpringPageVO<PromotionProductVo> findAll(@RequestBody PromotionProductReqVo reqVo){
        return commodityService.findAll(reqVo);
    }

    @ApiOperation("通过条形码 查找商品Id 会返回null")
    @GetMapping("findProductIdByBarCode")
    Long findProductIdByBarCode(@RequestParam("barCode") String barCode){
        return commodityService.findProductIdByBarCode(barCode);
    }

    @ApiOperation("获取商品规格数据")
    @GetMapping("getRelevant")
    List<ProductRelevantDetail> getRelevant(@RequestParam("productId") Long productId){
        return commodityService.getRelevant(productId);
    }


    @ApiOperation("通过分类Id获取搜索条件")
    @GetMapping("findFieldByCategoryId")
    List<ProductFieldVo> findFieldByCategoryId(@RequestParam("findFieldByCategoryId") Long categoryId){
        return extendService.findFieldByCategoryId(categoryId);
    }

    @ApiOperation("获取搜索权重")
    @GetMapping("config")
    KeywordWeightVo getConfig(){
        return keywordWeightService.getConfig();
    }


    @ApiOperation("获取类型商品所有商品编码")
    @GetMapping("getTypeProductCodes")
    List<String> getTypeProductCodes(@RequestParam("productType") VendorType vendorType){
        return commodityService.getTypeProductCodes(vendorType);
    }

    @GetMapping("findCommodityByCode")
    CommodityVo findCommodityByCode(@RequestParam("productCode") String productCode){
        return commodityService.findCommodityByCode(productCode);
    }

    @PostMapping("findCommoditiesByCodes")
    List<CommodityVo> findCommoditiesByCodes(@RequestBody List<String> productCodes){
        return commodityService.findCommodityByCodes(productCodes);
    }

    @PostMapping("findByCodesFromDb")
    List<CommodityVo> findByCodesFromDb(@RequestBody List<String> codes){
        return commodityService.findByCodesFromDb(codes);
    }

    @PostMapping("syncCommoditySepcs")
    void syncCommoditySepcs(@RequestBody List<SyncCommoditySpecVo> reqVos){
        commodityService.syncCommoditySepcs(reqVos);
    }

    @PostMapping("syncCommodities")
    void syncCommodities(@RequestBody List<SyncCommodityVo> reqVos){
        commodityService.syncCommodities(reqVos);
    }
}
